import 'package:flutter/material.dart';
import 'package:module_home/const/color.dart';
import 'package:module_home/const/string.dart';
import 'package:module_home/model/ISSDataEntity.dart';

class IssDetailItem extends StatefulWidget {
  final GrandChildList grandChildList;
  final ChildList childList;

  const IssDetailItem({Key key, this.childList, this.grandChildList})
      : super(key: key);

  @override
  _IssDetailItemState createState() => _IssDetailItemState();
}

class _IssDetailItemState extends State<IssDetailItem> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
          decoration: new BoxDecoration(
              border: new Border.all(
                  color: (widget.grandChildList.check == 0)
                      ? Colors.transparent
                      : Colors.redAccent,
                  width: 0.5),
              color: (widget.grandChildList.check == 0)
                  ? Colors.grey[300]
                  : Colors.yellow[100],
              borderRadius: new BorderRadius.circular((5.0))),
          padding: EdgeInsets.only(bottom: 10),
          margin: EdgeInsets.all(10),
          child: Column(children: [
            Container(
              height: 48,
              alignment: Alignment.centerLeft,
              child: Row(
                children: [
                  Container(
                    decoration: new BoxDecoration(
                        color: _buildColor(widget.grandChildList.value),
                        borderRadius: new BorderRadius.circular((2.0))),
                    margin: EdgeInsets.only(left: 2, right: 10),
                    height: 30.0,
                    width: 4,
                  ),
                  Expanded(
                      child: Text(
                    severity + widget.grandChildList.value.toString() + minute,
                    style: const TextStyle(color: Colors.black87),
                  )),
                  Checkbox(
                    value: (widget.grandChildList.check == 0) ? false : true,
                    //选中时的颜色
                    activeColor: Colors.red,
                    onChanged: (value) {
                      _buildData();
                    },
                  ),
                ],
              ),
            ),
            Column(
              children: _buildGroup(widget.grandChildList),
              crossAxisAlignment: CrossAxisAlignment.start,
            ),
          ])),
      onTap: () {
        _buildData();
      },
    );
  }

  _buildData() {
    setState(() {
      widget.grandChildList.check == 1
          ? widget.grandChildList.check = 0
          : widget.grandChildList.check = 1;
    });
  }

  Color _buildColor(int _value) {
    switch (_value) {
      case 1:
        return CustomColors.color1;
      case 2:
        return CustomColors.color2;
      case 3:
        return CustomColors.color3;
      case 4:
        return CustomColors.color4;
      case 5:
        return CustomColors.color5;
      case 6:
        return CustomColors.color6;
      default:
        return CustomColors.color1;
    }
  }

  List<Widget> _buildGroup(GrandChildList group) {
    return group.sonLists.map((item) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(left: 10, top: 5),
                child: Image.asset(
                  "images/compute_icon_triangle.png",
                  width: 12,
                  height: 13,
                ),
              ),
              Expanded(
                  child: Container(
                margin: EdgeInsets.only(right: 10),
                child: Text(item.sontitle, softWrap: true),
              )),
            ],
          ),
          Container(
              child: Column(
            children: _buildItem(item.soncontent),
            crossAxisAlignment: CrossAxisAlignment.start,
          ))
        ],
      );
    }).toList();
  }

  List<Widget> _buildItem(List<String> items) {
    if (items == null || items.length == 0) {
      return [];
    }
    return items.map((item) {
      return Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 20, top: 5),
            child: Image.asset(
              "images/compute_icon_triangle_null.png",
              width: 12,
              height: 13,
            ),
          ),
          Expanded(
            child: Container(
              margin: EdgeInsets.only(right: 10),
              child: Text(item, softWrap: true),
            ),
          ),
        ],
      );
    }).toList();
  }
}
